const app = getApp()
Page({
  data: {
    // 轮播图数据
    // 新闻列表数据
	slides:[],
    searchKeyword: '',
    newsList: [
    ]
  },
  onLoad() {
	  const self = this
	  	wx.checkSession({
	  		success() { 
	  			app.util.getUserInfo(function (response) {
	  				console.log("==获取当前用户信息1==");
	  				console.log(response);
	  				self.setData({
	  					userInfo: response
	  				});
	  				console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
	  				
	  				setTimeout(function(){
	  					self.loadData()
	  				},500)
	  			});
	  		},
	  		fail() {
				
				app.util.getUserInfo(function (response) {
					console.log("==获取当前用户信息1==");
					console.log(response);
					self.setData({
						userInfo: response
					});
					console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
					
					setTimeout(function(){
						self.loadData()
					},500)
				});
	  		}
	  	});
  },
  async loadData() {
  
    var that = this
	app.util.request({
		url: 'Bjjy/bjjylists',
		data: {
			keyword: that.data.searchKeyword
		},
		success(res) {
			var data = res.data.data;
			var list = data.bjjy
			that.setData({
			  newsList: list,
			  slides: data.slide
			});
			// wx.setStorageSync('newlist',list)
		}
	})
  },
  
  // 搜索输入
  onSearchInput: debounce(function(e) {
    this.setData({ searchKeyword: e.detail.value.trim() })
    this.loadData(true)
  }, 500),
  
  // 清空搜索
  clearSearch() {
    this.setData({ searchKeyword: '' })
    this.loadData(true)
  },
  onSlideTap:function(e){
          var slide = this.data.slides[e.currentTarget.dataset.index];
          if(!slide){
                  return;
          }
          if(slide.stype==1){
                  wx.navigateTo({
                    url: slide.stype_link,
                  });
          }else if(slide.stype==2){
                  wx.navigateTo({
                          url: 'html?cmd=slide&id='+slide.id
                  });
          }else if(slide.stype==3){
                  console.log(slide.stype_link);
                  var link=slide.stype_link.split(":");
                  console.log(link);
                  wx.navigateToMiniProgram({
                          appId: link[0],
                          path: link[1]
                  });
          }else if(slide.stype==4){
                  console.log(slide.stype_link);
                  var link=slide.stype_link.split(":");
                  console.log(link);
                  wx.openChannelsActivity({
                          finderUserName: link[0],
                          feedId: link[1]
                  });
          }
  },
  onNewsItemTap(e) {
    const newsId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/vp_pz/pages/bjjy/detail?id=${newsId}`
    });
  }
});

  // 防抖函数
  function debounce(fn, delay) {
    let timer = null
    return function(...args) {
      clearTimeout(timer)
      timer = setTimeout(() => fn.apply(this, args), delay)
    }
  }